package apiWeb

import (
	vodResource "ZHONGYIHANGYAN/business/vodResource"
	"fmt"

	"ZHONGYIHANGYAN/web"

	requset "ZHONGYIHANGYAN/bean/request"

	"github.com/gin-gonic/gin"
)

func (api *Api) VodResourceRouters(g *gin.RouterGroup) {
	routerG := g.Group("/vodResource")
	{
		// 获取点播资源列表
		routerG.POST("/getVodResourceList", web.NewWebRouter(api.getVodResourceList).Func)
		// 添加或修改点播资源
		routerG.POST("/addOrUpdateVodResource", web.NewWebRouter(api.addOrUpdateVodResource).Func)
		// 删除点播资源
		routerG.POST("/deleteVodResource", web.NewWebRouter(api.deleteVodResource).Func)
		// 获取点播资源详情
		routerG.POST("/getVodResourceDetail", web.NewWebRouter(api.getVodResourceDetail).Func)
		// 下载资源
		routerG.POST("/downloadVodResource", web.NewWebRouter(api.downloadVodResource).Func)
	}
}

func (api *Api) InitVodResourceRouter() error {
	api.vodResourceManage = vodResource.Manage()
	api.web.RegRouters(api.VodResourceRouters)
	return nil
}

// 获取点播资源列表
func (api *Api) getVodResourceList(c *gin.Context, replayData *interface{}) error {
	query := requset.GetVodResourceListQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	infoList, err := api.vodResourceManage.GetVodResourceList(&query)
	if err != nil {
		return err
	}
	*replayData = infoList
	return nil
}

// 添加或修改点播资源
func (api *Api) addOrUpdateVodResource(c *gin.Context, replayData *interface{}) error {
	info := requset.AddOrUpdateVodResourceQuery{}
	if err := api.web.GetPayload(c, &info); err != nil {
		return err
	}
	if err := api.vodResourceManage.AddOrUpdateVodResource(&info); err != nil {
		return err
	}
	return nil
}

// 删除点播资源
func (api *Api) deleteVodResource(c *gin.Context, replayData *interface{}) error {
	query := requset.DeleteVodResourceQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	if len(query.VodResourceIds) == 0 {
		return fmt.Errorf("请传入需要删除的点播资源id")
	}
	if err := api.vodResourceManage.DeleteVodResource(query.VodResourceIds); err != nil {
		return err
	}
	return nil
}

// 获取点播资源详情
func (api *Api) getVodResourceDetail(c *gin.Context, replayData *interface{}) error {
	query := requset.GetVodResourceDetailQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	info, err := api.vodResourceManage.GetVodResourceDetail(&query)
	if err != nil {
		return err
	}
	*replayData = info
	return nil
}

// 下载资源
func (api *Api) downloadVodResource(c *gin.Context, replayData *interface{}) error {
	query := requset.DownloadVodResourceQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	result, err := api.vodResourceManage.DownloadVodResource(&query)
	if err != nil {
		return err
	}
	*replayData = result
	return nil
}